import 'package:fl_amap/fl_amap.dart';
import 'package:flutter_ncsjqz_getx/http/dio_service.dart';
import 'package:flutter_ncsjqz_getx/model/commodity_list_entity.dart';
import 'package:flutter_ncsjqz_getx/model/index_page_img_list_entity.dart';
import 'package:flutter_ncsjqz_getx/model/shop_home_category_entity.dart';
import 'package:flutter_ncsjqz_getx/util/fl_amap_util.dart';
import 'package:flutter_ncsjqz_getx/widget/drop_down_button_widget.dart';
import 'package:get/get.dart';

class FarmLogic extends GetxController {
  String? local = "未知位置";
  LatLong? latLong;
  DropDownButtonModel? downButtonModel = DropDownButtonModel(name: "综合排序",value: 1);
  List<String> type = [];
  List<int> typeId = [];
  int currentIndex = 0;
  List<DropDownButtonModel> listDropDownButtonModel = [
    DropDownButtonModel( name: "综合排序",value: 1,),
    DropDownButtonModel( name: "由近到远",value: 7,),
    DropDownButtonModel( name: "由远到近",value: 6,),
  ];
  /// 轮播图数据
  List<String>? images = [];
  /// 高德定位对象
  AMapLocation aMapLocation = AMapLocation();
  /// 商品数据
  List<CommodityListDataContent>? commodityListDataContent = [];
  /// 搜索值
  String searchKey = "";
  ///  1综合排序 2远到近 3近到远
  int sort = 1;
  ///  索引
  int currPage = 1;
  setCurrentIndex(value){
    currentIndex = value;
    getCommodityList();
    update();
  }
  setLocal(value){
    local = value;
    update();
  }

  setDrop(value){
    downButtonModel = value;
    update();
  }

  /// 获取轮播图
  getImageList() async{
    IndexPageImgListEntity indexPageImgListEntity = await DioService<IndexPageImgListEntity>().queryCarouselList();
    images = indexPageImgListEntity.data;
    update();
  }

  /// 获取农场列表
  /// load ? 加载更多 : 刷新
  getCommodityList({load:false}) async{
    load ? currPage++ : currPage = 1;
    Map<String,dynamic> map = {
      "pageSize": 10 ,
      "currPage": currPage,
      "status": 1,// 默认是1，表示正常状态, 2-下架
      "sort": downButtonModel!.value, // 排序
    };
    if(typeId.length > 0){
      map["parentCategoryId"] = typeId[currentIndex];
    }
    if(latLong!=null){
      map["lon"] = latLong!.longitude;
      map["lat"] = latLong!.latitude;
    }
    if(searchKey != "") map["keyword"] = searchKey;// 搜索
    CommodityListEntity commodityListEntity = await DioService<CommodityListEntity>().indexSearch(map);
    if(commodityListEntity.code == 0){
      if(load) return commodityListDataContent!.addAll(commodityListEntity.data!.content!);
      commodityListDataContent = [];
      commodityListDataContent = commodityListEntity.data!.content;
    }
    update();
  }

  /// 获取分类
  getType() async{
    ShopHomeCategoryEntity commodityListEntity = await DioService<ShopHomeCategoryEntity>().commodityCategory();
    if(commodityListEntity.code == 0){
      commodityListEntity.data!.forEach((element) {
        type.add(element.categoryName!);
        typeId.add(element.categoryId!);
      });
      update();
    }
  }

  /// 获取位置
  getLocation() async{
    await FlAmapUtil.initialize(); // 初始化
    aMapLocation = await FlAmapUtil.getLocation() ?? AMapLocation(); // 获取定位
    if(aMapLocation.code!=12){
      local = aMapLocation.province! + aMapLocation.city!;
      latLong = aMapLocation.latLong;
    }
    update();
  }

  /// 关闭定位
  closeLocation(){
    FlAmapUtil.dispose();
  }
}
